@push('topic_social_share')
<script>
    $(document).ready(function () {
        var $config = {
            title: document.title + ' | from DS #dspstack.com# {{ $topic->user->id != 1 ? '@AllenZhangFive' : '' }} {{ $topic->user->weibo_name ? '@'.$topic->user->weibo_name : '' }}',
            wechatQrcodeTitle: "微信扫一扫：分享", // 微信二维码提示文字
            wechatQrcodeHelper: '<p>微信里点“发现”，扫一下</p><p>二维码便可将本文分享至朋友圈。</p>',
            image: "{{ $cover ? $cover->link : 'https://dspstack.com/uploads/images/201701/29/1/pQimFCe1r5.png' }}",
            sites: ['weibo', 'wechat', 'facebook', 'twitter', 'google', 'qzone', 'qq', 'douban'],
        };

        socialShare('.social-share-cs', $config);

        Config.following_users = @if($currentUser) {!!$currentUser->present()->followingUsersJson()!!} @else [] @endif;
        PHPHub.initAutocompleteAtUser();
    });
</script>

@endpush

@extends('layouts.default')

@section('title')
    {{{ $topic->title }}} | @parent
@stop

@section('description')
    {{{ $topic->excerpt }}}
@stop

@section('wechat_icon')
    @if ($cover = $topic->cover())
        <img src="{{ img_crop($cover->link, 512, 512) }}" alt="">
    @else
        @parent
    @endif
@stop

@section('content')

    <div class="col-md-9 topics-show main-col">
        <div id='topic-knot-info' class="alert alert-info hidden">
            结帖说明：<br>
            请在所有评论中勾选您满意的答案，最多为2个不同的用户。勾选完毕后，请点击
            <a class="btn btn-primary no-pjax action-btn" href="javascript:void(0)" onclick="satisfied_knot(1)">满意结帖</a>，如果没有找到满意的答案，
            我要进行<a class="btn btn-primary no-pjax action-btn" href="javascript:void(0)" onclick="satisfied_knot(0)">不满意结帖</a>
        </div>

        <div id='topic-knot-warning' class="alert alert-warning hidden">

        </div>
        <div id='topic-knot-success' class="alert alert-success hidden">

        </div>

        <!-- Topic Detial -->
        <div class="topic panel panel-default">
            <div class="infos panel-heading">

                <h1 class="panel-title topic-title">
                    @if ($topic->isShareLink())
                        <a href="{{ $topic->share_link->link }}" target="_blank">
                            <i class="fa fa-link"></i>
                        </a>
                    @endif
                    {{{ $topic->title }}}
                </h1>

                @include('topics.partials.meta')
            </div>

            <div class="content-body entry-content panel-body ">

                @include('topics.partials.body', array('body' => $topic->body))
                @if(isset($topic->resource))
                    <div class="panel-body vote-box text-center">
                        <div class="btn-group">
                            <a href="{{ route('resources.download', ['id' => $topic->resource->id]) }}"
                               data-content="需要 @if($topic->resource->cost_type == 1) {{ $topic->resource->cost }} 积分 @elseif($topic->resource->cost_type == 2) {{ $topic->resource->cost }} D币 @endif "
                               class="vote btn btn-primary popover-with-html"
                               target="_blank"
                            >
                                <i class="fa fa-download"></i>&nbsp;下载
                            </a>
                            {{--@if(($category->id == config('phphub.algorithm_sources_id')--}}
                            {{--|| $category->id == config('phphub.resources_id'))--}}
                            {{--&& Auth::check() && !Auth::user()->isVip())--}}
                                {{--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--}}
                                {{--<a href="{{route('vip-index')}}">--}}
                                    {{--<button type="button" class="btn btn-danger"> 开通VIP</button>--}}
                                {{--</a>--}}
                            {{--@endif--}}
                        </div>
                    </div>
                @endif
                <div class="meta">
                    <i class="icon fa fa-tags"></i>
                    @foreach($topic->tags as $tag)
                        <a class="ui topic-label" href="{{ route('tags.show', ['id' => $tag->id]) }}"
                           target="_blank">{{ $tag->name }}</a>
                    @endforeach
                </div>
                <div data-lang-excellent="{{ lang('This topic has been mark as Excenllent Topic.') }}"
                     data-lang-wiki="{{ lang('This is a Community Wiki.') }}" class="ribbon-container">
                    @include('topics.partials.ribbon')
                </div>
            </div>
            <div class="appends-container" data-lang-append="{{ lang('Append') }}">
                @foreach ($topic->appends as $index => $append)

                    <div class="appends">
                        <span class="meta">{{ lang('Append') }} {{ $index }} &nbsp;·&nbsp; <abbr
                                    title="{{ $append->created_at }}"
                                    class="timeago">{{ $append->created_at }}</abbr></span>
                        <div class="sep5"></div>
                        <div class="markdown-reply append-content">
                            {!! $append->content !!}
                        </div>
                    </div>

                @endforeach
            </div>
            @if($revisionHistory && in_array($revisionHistory->key, ['is_excellent', 'order']))
                <div class="admin-operation">
                    <?php
                    $revisionAdmin = \App\Models\User::find($revisionHistory->user_id);
                    $adminOperation = '';
                    if ($revisionHistory->key == 'is_excellent') {
                        $adminOperation = $revisionHistory->new_value == 'yes' ? '加精' : '解除加精';
                    }

                    if ($revisionHistory->key == 'order') {
                        if ($revisionHistory->new_value < 0) {
                            $adminOperation = '沉帖';
                        } elseif ($revisionHistory->new_value > 0) {
                            $adminOperation = '置顶';
                        } elseif ($revisionHistory->new_value == 0) {
                            $adminOperation = $revisionHistory->old_value > 0 ? '取消置顶' : '取消沉帖';
                        }
                    }
                    ?>
                    @if($adminOperation)
                        本帖由 <a href="{{route('users.show', $revisionAdmin->id)}}"
                               target="_blank">{{$revisionAdmin->name}}</a>
                        于 {{$revisionHistory->created_at->diffForHumans()}} {{$adminOperation}}
                    @endif
                </div>
            @endif
            @include('topics.partials.topic_operate', ['manage_topics' => $currentUser ? ($currentUser->can("manage_topics") && $currentUser->roles->count() <= 5) : false])
        </div>


        @include('topics.partials.show_segment')

    </div>

    @if( $topic->user->payment_qrcode )
        @include('topics.partials.payment_qrcode_modal')
    @endif

    @if($topic->isTranslate())
        @if($topic->isTranslated())
            @include('translate.partials.show_sidebar_off')
        @else
            @include('translate.partials.show_sidebar_on')
        @endif

    @else
        @include('layouts.partials.sidebar')
    @endif

    @include('layouts.partials.bottombanner')

@stop

@section('scripts')

    <script>
        $('#topic-knot-button').click(function () {
            if ( {{$topic->status}} != 0)
            {
                $('#topic-knot-warning').html('该话题已结贴，不能重复结帖');
                $('#topic-knot-warning').removeClass('hidden');
                window.setTimeout(function () {
                    $('#topic-knot-warning').addClass('hidden');
                }, 2000);
                return;
            }
            if ($('#topic-knot-info').hasClass('hidden')) {
                $('#topic-knot-info').removeClass('hidden');
                $('.satisfied-check-item').removeClass('hidden');
            } else {
                $('#topic-knot-info').addClass('hidden');
                $('.satisfied-check-item').addClass('hidden');
            }
        });

        function satisfied_knot(flag) {
            //获取checkbox, 并提到后端
            var user_ids = [];
            var replies_ids = [];
            //var topic_ids = [];
            $('input[name="checkbox"]:checked').each(function () {
                user_ids.push($(this).val().split('-')[0])
                replies_ids.push($(this).val().split('-')[1])
                //topic_ids.push($(this).val().split('-')[2])
            });
            if (flag == 1 && (user_ids.length == 0 || replies_ids.length == 0)) {
                $('#topic-knot-warning').html('您选择的是满意结帖，请在评论区对满意答案打钩');
                $('#topic-knot-warning').removeClass('hidden');
                window.setTimeout(function () {
                    $('#topic-knot-warning').addClass('hidden');
                }, 5000);
                return;
            }
            $.ajax({
                url: "{{url('/topics/knot_homework')}}",
                type: 'POST',
                dataType: 'json',
                data: {
                    user_ids: user_ids,
                    replies_ids: replies_ids,
                    topic_id:{{$topic->id}},
                    flag: flag
                },
                success: function (data) {
                    if (data.status == 1) {
                        $('#topic-knot-success').html('结帖成功,正在刷新页面...');
                        $('#topic-knot-success').removeClass('hidden');
                        setTimeout(function () {
                            window.location.reload(true);
                        }, 2000);
                    }
                },
                error: function (e) {

                }

            });
        }
    </script>
@stop
